<template>
  <div style="min-height: 76vh">
    <el-form @keyup.enter.native="getData()" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="业务名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地区选择"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import SelectAddress from '@/components/select/SelectAddress'
export default {
  components: {
    SelectAddress,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 20,
        dam_name: '',
        address_code: '',
      },
      loading: false,
      tableCount: 0,
      tableName: 'class_project_total_view_cg',
      tableData: [],
      tableColumns: [
        { attrs: { label: '项目名称', prop: 'customer_name' } },
        // { attrs: { label: '地址', prop: 'address' } },
        // { attrs: { label: '申请项目', prop: 'apply_project_total' } },
        { attrs: { label: '申请金额', prop: 'apply_funds_total' } },
        // { attrs: { label: '贷款项目', prop: 'apply_funds_total' } },
        { attrs: { label: '放款金额', prop: 'reply_funds_total' } },
        { attrs: { label: '在保余额', prop: 'left_funds_total' } },
      ],
    }
  },
  computed: {},
  watch: {},
  created() {
    this.params.address_code = this.$utils.getAddressCode()
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.getCount()
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableData = data.data || []
      this.tableCount = data.totalCount || 0
    },
    getCount() {
      const { index, size, ...params } = this.params
      params.groupFunc = `sum(${this.tableName}.apply_funds_total),sum(${this.tableName}.reply_funds_total),sum(${this.tableName}.left_funds_total)`
      this.$api.getData(this.$serviceName, this.tableName, params).then(res => {
        const sum = (res && res.data && res.data[0]) || {}
        this.$set(this.tableColumns[1].attrs, 'label', `申请金额 (${sum.p || 0}万元)`)
        this.$set(this.tableColumns[2].attrs, 'label', `放款金额 (${sum.p1 || 0}万元)`)
        this.$set(this.tableColumns[3].attrs, 'label', `在保余额 (${sum.p2 || 0}万元)`)
      })
    },
  },
}
</script>
<style lang="scss" scoped></style>
